<?php

/**
 * 病种管理表
 */

namespace app\common\model;

use app\common\model\search\DiseaseSearch;
use think\Paginator;

class Disease extends BaseModel
{
    use DiseaseSearch;

    protected $name = "disease";

    /**
     * 获取数据列表
     * @return Paginator
     */
    public function getLists(): Paginator
    {
        return $this->mongoSearch()->paginate($this->pageLimit);
    }

    /**
     * 添加数据
     * @param array $data
     * @return string
     */
    public function addData(array $data): string
    {
        $data['type'] = $data['type'] ?? 1;
        //添加时销毁自带的主键id
        unset($data['id']);
        return $this->save($data);
    }

    /**
     * 更新数据
     * @param array $data
     * @return bool
     */
    public function saveData($id, array $data): bool
    {
        $model = self::find($id);
        return $model->save($data);
    }

    /**
     * 获取全部数据
     * @return array
     */
    public function getRows(): array
    {
        return $this->field('illnesscode,name')
            ->limit(100)
            ->select()
            ->each(function ($item, $key) {
                $item['id'] = $item['illnesscode'];
                unset($item['illnesscode']);
                return $item;
            })
            ->toArray();
    }

    /**
     * 获取条件匹配的数据
     * @param array $where 条件
     * @param string $field 字段
     * @param string $sort 排序
     * @return array
     */
    public function getConditionData(array $where = [], string $field = "*", string $sort = "asc"): array
    {
        return $this->field($field)->where($where)->order('id', $sort)->select()->toArray();
    }
}